package com.nCoV.EpidemicSurveillance.repository;

import java.util.List;
import java.util.Map;

import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Component;

import com.nCoV.EpidemicSurveillance.domain.PatientTrainRelation;
import com.nCoV.EpidemicSurveillance.domain.StudentPlaceRelationship;
import com.nCoV.EpidemicSurveillance.domain.TrainNode;

/**
 * @author 罗穆健
 * @date 2020年4月7日
 */
@Component
public interface TrafficRepository extends Neo4jRepository<StudentPlaceRelationship, Long> {

	@Query("merge (t:Traffic{trainNumber::#{#trainNode.trainNumber}}) "
			+ "set t.startCity=:#{#trainNode.startCity} "
			+ "set t.endCity=:#{#trainNode.endCity} "
			+ "set t.trafficType=:#{#trainNode.trafficType}")
	void createTrain(TrainNode trainNode);

	@Query("match(pat:Person{idCard::#{#patientIdCard}}) "
			+ "match(traffic:Traffic{trainNumber::#{#trainNumber}}) "
			+ "merge (pat)-[r:TrafficRelation]->(traffic)  "
			+ "set r.contactName=:#{#patientTrainRelation.contactName} "
			+ "set r.startStation=:#{#patientTrainRelation.startStation} "
			+ "set r.endStation=:#{#patientTrainRelation.endStation} "
			+ "set r.startTime =:#{#patientTrainRelation.startTime}"
			+ "set r.endTime =:#{#patientTrainRelation.endTime}")
	void createTrainRelation(String patientIdCard, PatientTrainRelation patientTrainRelation, String trainNumber);

	@Query("match(n:Person{idCard::#{#patientIdCard}})-[R:TrafficRelation]->(t:Traffic) "
			+ "where t.trainNumber =~:#{#searchTrafficData} "
			+ "and R.contactName in ['byTrain'] "
			+ "return count(*) as count")
	int getAreaRelationCount(String patientIdCard, String searchTrafficData);

	
	@Query("match(n:Person{idCard::#{#patientIdCard}})-[R:TrafficRelation]->(t:Traffic) "
			+ "where t.trainNumber =~:#{#searchTrafficData} "
			+ "and R.contactName in ['byTrain'] "
			+ "return R.contactName as contactName, "
			+ "R.startStation as startStation, "
			+ "R.endStation as endStation,"
			+ "R.startTime as startTime,"
			+ "R.endTime as endTime,"
			+ "t.trainNumber as trainNumber,"
			+ "t.trafficType as trafficType,"
			+ "t.startCity as startCity,"
			+ "t.endCity as endCity, "
			+ "id(t) as trafficId, "
			+ "id(R) as relationId "
			+ "skip :#{#skipNum} "
			+ "limit :#{#limitNum}")
	List<Map<String, Object>> queryAreaByRelation(String patientIdCard, String searchTrafficData, int skipNum,
			int limitNum);

	@Query("Match ()-[r]-() Where ID(r)= :#{#relationId} Delete r")
	void deleteTrafficRelation(Long relationId);

	@Query("Match ()-[r]-(n) where id(n)= :#{#trafficId} and id(r) = :#{#patientTrainRelation.id} "
			+ "set r.startStation = :#{#patientTrainRelation.startStation} "
			+ "set r.endStation = :#{#patientTrainRelation.endStation} "
			+ "set r.startTime = :#{#patientTrainRelation.startTime} "
			+ "set r.endTime = :#{#patientTrainRelation.endTime} ")
	void updatedTraffic(PatientTrainRelation patientTrainRelation, Long trafficId);

	@Query("match (n:Person)-[r:TrafficRelation]->(m:Traffic) where n.grgl ='1.0' and m.trainNumber =~ :#{#trainNumber} "
			+ "return m.trainNumber as trainNumber,m.startCity as startCity, m.endCity as endCity,"
			+ "r.startStation as startStation, r.endStation as endStation,"
			+ "r.endTime as endTime,r.startTime as startTime "
			+ "skip :#{#skipNum} "
			+ "limit :#{#limitNum}")
	List<Map<String, Object>> getTrafficData(String trainNumber, int skipNum, int limitNum);
	
	@Query("match (n:Person)-[r:TrafficRelation]->(m:Traffic) "
			+ "where n.grgl ='1.0' and m.trainNumber =~ :#{#trainNumber} "
			+ "return count(*) as count")
	int getTrafficCount(String trainNumber);

	

	
}
